package com.bifang.common.util;

import com.bifang.common.exception.AppException;
import com.bifang.common.vo.AntTreeVO;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.util.CollectionUtils;

/**
 * ant vue树工具类
 *
 * @author chenzh
 * @date 2018-08-14 *
 */
public class AntTreeUtil {
    public static List<AntTreeVO> constructTaskDTOToTree(List<AntTreeVO> taskDTOList) {
        return constructTaskDTOToTree(taskDTOList, null);
    }

    public static List<AntTreeVO> constructTaskDTOToTree(
            List<AntTreeVO> taskDTOList, Integer nodeLevel, Integer expandedLevel) {
        List<AntTreeVO> list = constructTaskDTOToTree(taskDTOList, null);
        return getRetList(nodeLevel, expandedLevel, list);
    }

    public static List<AntTreeVO> constructTaskDTOToTree(
            List<AntTreeVO> taskDTOList,
            String resultTask,
            Integer nodeLevel,
            Integer expandedLevel) {
        List<AntTreeVO> list = constructTaskDTOToTree(taskDTOList, resultTask);
        return getRetList(nodeLevel, expandedLevel, list);
    }

    private static List<AntTreeVO> getRetList(
            Integer nodeLevel, Integer expandedLevel, List<AntTreeVO> treeList) {
        if (nodeLevel == null && expandedLevel == null) {
            return treeList;
        }
        if (nodeLevel != null) {
            if (expandedLevel == null) {
                expandedLevel = nodeLevel;
            } else {
                if (expandedLevel > nodeLevel) {
                    expandedLevel = nodeLevel;
                }
            }
            nodeLevel = nodeLevel - 1;
        }
        if (expandedLevel != null) {
            expandedLevel = expandedLevel - 1;
        } else {
            expandedLevel = -1;
        }

        List<AntTreeVO> retList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(treeList)) {
            for (AntTreeVO antTreeVO : treeList) {
                if ((nodeLevel != null && nodeLevel > 0) || expandedLevel > 0) {
                    if (nodeLevel != null && nodeLevel > 0) {
                        antTreeVO.setLoaded(true);
                    }
                    if (expandedLevel > 0) {
                        antTreeVO.setExpanded(true);
                    }
                    if (!CollectionUtils.isEmpty(antTreeVO.getChildren())) {
                        antTreeVO.setChildren(
                                getRetList(nodeLevel, expandedLevel, antTreeVO.getChildren()));
                    } else {
                        if (nodeLevel != null && nodeLevel > 0) {
                            antTreeVO.setIsLeaf(true);
                        }
                    }

                    retList.add(antTreeVO);
                } else {
                    if (nodeLevel != null && nodeLevel <= 0) {
                        antTreeVO.setChildren(new ArrayList<>());
                    }
                    retList.add(antTreeVO);
                }
            }
        }
        return retList;
    }
    /**
     * 将List重组为数
     *
     * @param taskDTOList DTO集合
     * @return List<TreeEntity>
     */
    public static List<AntTreeVO> constructTaskDTOToTree(
            List<AntTreeVO> taskDTOList, String resultTask) {
        if (CollectionUtils.isEmpty(taskDTOList)) {
            return new ArrayList<>();
        }
        // key:父节点ID value:子节点集合
        Map<String, List<AntTreeVO>> taskDTOMap = new HashMap<>();

        // 将List重组到Map中
        taskDTOList.forEach(
                dto -> {
                    List<AntTreeVO> tempTaskDTOList = taskDTOMap.get(dto.getParentKey());
                    if (tempTaskDTOList == null) {
                        tempTaskDTOList = new ArrayList<AntTreeVO>();
                        taskDTOMap.put(dto.getParentKey(), tempTaskDTOList);
                    }
                    tempTaskDTOList.add(dto);
                });

        // 顶级节点集合

        List<AntTreeVO> resultTaskDTOList = taskDTOMap.get(resultTask);
        if (CollectionUtils.isEmpty(resultTaskDTOList)) {
            resultTaskDTOList = taskDTOMap.get("#");
        }

        recurTaskDTOList(resultTaskDTOList, taskDTOMap);

        return resultTaskDTOList;
    }

    /**
     * 将重组好的Map进行树形结构处理
     *
     * @param taskDTOList 父节点集合(不一定是顶级节点 因为会递归调用)
     * @param sourceMap 组装好的Map集合
     */
    public static void recurTaskDTOList(
            List<AntTreeVO> taskDTOList, Map<String, List<AntTreeVO>> sourceMap) {
        if (CollectionUtils.isEmpty(taskDTOList)) {
            return;
        }
        taskDTOList.forEach(
                dto -> {
                    dto.setChildren(sourceMap.get(dto.getKey()));
                    recurTaskDTOList(dto.getChildren(), sourceMap);
                });
    }

    public static List<String> getChildKeys(List<AntTreeVO> taskDTOList, String key) {
        List<String> list = new ArrayList<>();
        if (CollectionUtils.isEmpty(taskDTOList)) {
            throw new AppException("未传入数据");
        }
        getChildKeys(taskDTOList, key, list, false);
        return list;
    }

    public static boolean getChildKeys(
            List<AntTreeVO> taskDTOList, String key, List<String> list, boolean flag) {
        if (flag) {
            for (AntTreeVO antTreeVO : taskDTOList) {
                list.add(antTreeVO.getKey());
                if (!CollectionUtils.isEmpty(antTreeVO.getChildren())) {
                    getChildKeys(antTreeVO.getChildren(), key, list, flag);
                }
            }
        } else {
            for (AntTreeVO antTreeVO : taskDTOList) {
                if (antTreeVO.getKey().equalsIgnoreCase(key)) {
                    list.add(key);
                    flag = true;
                    if (!CollectionUtils.isEmpty(antTreeVO.getChildren())) {
                        getChildKeys(antTreeVO.getChildren(), key, list, flag);
                    }
                    break;
                }
            }
            if (!flag) {
                for (AntTreeVO antTreeVO : taskDTOList) {
                    if (!CollectionUtils.isEmpty(antTreeVO.getChildren())) {
                        if (getChildKeys(antTreeVO.getChildren(), key, list, flag)) {
                            break;
                        }
                    }
                }
            }
        }
        return flag;
    }

    public static List<String> getParentKeys(List<AntTreeVO> taskDTOList, String key) {
        List<String> list = new ArrayList<>();
        if (CollectionUtils.isEmpty(taskDTOList)) {
            throw new AppException("未传入数据");
        }
        getParentKeys(taskDTOList, key, list);
        return list;
    }

    public static Boolean getParentKeys(
            List<AntTreeVO> taskDTOList, String key, List<String> list) {
        if (CollectionUtils.isEmpty(taskDTOList)) {
            return false;
        }
        boolean flag = false;
        for (AntTreeVO antTreeVO : taskDTOList) {
            if (antTreeVO.getKey().equalsIgnoreCase(key)) {
                flag = true;
                list.add(key);
                break;
            }
        }
        if (!flag) {
            for (AntTreeVO antTreeVO : taskDTOList) {
                if (!CollectionUtils.isEmpty(antTreeVO.getChildren())) {
                    flag = getParentKeys(antTreeVO.getChildren(), key, list);
                    if (flag) {
                        list.add(antTreeVO.getKey());
                        break;
                    }
                }
            }
        }
        return flag;
    }
}
